/**
 * 系统权限定义
 * 权限结构：菜单 -> 页面 -> 按钮/操作
 */

export const permissions = [
  {
    id: 'dashboard',
    name: '仪表板',
    icon: 'Odometer',
    type: 'menu',
    children: [
      {
        id: 'dashboard.view',
        name: '查看仪表板',
        type: 'page'
      }
    ]
  },
  {
    id: 'content',
    name: '内容管理',
    icon: 'Document',
    type: 'menu',
    children: [
      {
        id: 'articles',
        name: '文章管理',
        type: 'page',
        children: [
          { id: 'articles.view', name: '查看文章列表', type: 'action' },
          { id: 'articles.create', name: '创建文章', type: 'action' },
          { id: 'articles.edit', name: '编辑文章', type: 'action' },
          { id: 'articles.delete', name: '删除文章', type: 'action' },
          { id: 'articles.publish', name: '发布文章', type: 'action' },
          { id: 'articles.offline', name: '下线文章', type: 'action' }
        ]
      },
      {
        id: 'categories',
        name: '分类管理',
        type: 'page',
        children: [
          { id: 'categories.view', name: '查看分类列表', type: 'action' },
          { id: 'categories.create', name: '创建分类', type: 'action' },
          { id: 'categories.edit', name: '编辑分类', type: 'action' },
          { id: 'categories.delete', name: '删除分类', type: 'action' }
        ]
      },
      {
        id: 'tags',
        name: '标签管理',
        type: 'page',
        children: [
          { id: 'tags.view', name: '查看标签列表', type: 'action' },
          { id: 'tags.create', name: '创建标签', type: 'action' },
          { id: 'tags.edit', name: '编辑标签', type: 'action' },
          { id: 'tags.delete', name: '删除标签', type: 'action' }
        ]
      },
      {
        id: 'pages',
        name: '单页管理',
        type: 'page',
        children: [
          { id: 'pages.view', name: '查看单页列表', type: 'action' },
          { id: 'pages.create', name: '创建单页', type: 'action' },
          { id: 'pages.edit', name: '编辑单页', type: 'action' },
          { id: 'pages.delete', name: '删除单页', type: 'action' }
        ]
      }
    ]
  },
  {
    id: 'media',
    name: '媒体库',
    icon: 'Picture',
    type: 'menu',
    children: [
      {
        id: 'media.view',
        name: '查看媒体文件',
        type: 'page'
      },
      {
        id: 'media.upload',
        name: '上传文件',
        type: 'action'
      },
      {
        id: 'media.delete',
        name: '删除文件',
        type: 'action'
      },
      {
        id: 'media.batch_delete',
        name: '批量删除',
        type: 'action'
      }
    ]
  },
  {
    id: 'seo',
    name: 'SEO管理',
    icon: 'TrendCharts',
    type: 'menu',
    children: [
      {
        id: 'build',
        name: '静态生成',
        type: 'page',
        children: [
          { id: 'build.view', name: '查看生成页面', type: 'action' },
          { id: 'build.all', name: '生成所有页面', type: 'action' },
          { id: 'build.index', name: '生成首页', type: 'action' },
          { id: 'build.articles', name: '生成文章列表', type: 'action' },
          { id: 'build.article', name: '生成文章详情', type: 'action' },
          { id: 'build.category', name: '生成分类页', type: 'action' },
          { id: 'build.page', name: '生成单页面', type: 'action' },
          { id: 'build.logs', name: '查看生成日志', type: 'action' }
        ]
      },
      {
        id: 'sitemap',
        name: 'Sitemap生成',
        type: 'page',
        children: [
          { id: 'sitemap.view', name: '查看Sitemap页面', type: 'action' },
          { id: 'sitemap.generate_all', name: '生成所有格式', type: 'action' },
          { id: 'sitemap.generate_txt', name: '生成TXT', type: 'action' },
          { id: 'sitemap.generate_xml', name: '生成XML', type: 'action' },
          { id: 'sitemap.generate_html', name: '生成HTML', type: 'action' }
        ]
      }
    ]
  },
  {
    id: 'system',
    name: '系统管理',
    icon: 'Setting',
    type: 'menu',
    children: [
      {
        id: 'config',
        name: '基本信息',
        type: 'page',
        children: [
          { id: 'config.view', name: '查看配置', type: 'action' },
          { id: 'config.edit', name: '修改配置', type: 'action' }
        ]
      },
      {
        id: 'users',
        name: '用户管理',
        type: 'page',
        children: [
          { id: 'users.view', name: '查看用户列表', type: 'action' },
          { id: 'users.create', name: '创建用户', type: 'action' },
          { id: 'users.edit', name: '编辑用户', type: 'action' },
          { id: 'users.delete', name: '删除用户', type: 'action' },
          { id: 'users.reset_password', name: '重置密码', type: 'action' }
        ]
      },
      {
        id: 'roles',
        name: '角色管理',
        type: 'page',
        children: [
          { id: 'roles.view', name: '查看角色列表', type: 'action' },
          { id: 'roles.create', name: '创建角色', type: 'action' },
          { id: 'roles.edit', name: '编辑角色', type: 'action' },
          { id: 'roles.delete', name: '删除角色', type: 'action' },
          { id: 'roles.set_permissions', name: '设置权限', type: 'action' }
        ]
      }
    ]
  }
]

/**
 * 获取所有权限ID
 */
export function getAllPermissionIds() {
  const ids = []

  function traverse(items) {
    items.forEach(item => {
      ids.push(item.id)
      if (item.children) {
        traverse(item.children)
      }
    })
  }

  traverse(permissions)
  return ids
}

/**
 * 获取权限树（用于权限选择器）
 */
export function getPermissionTree() {
  return permissions
}

/**
 * 扁平化权限列表
 */
export function getFlatPermissions() {
  const flat = []

  function traverse(items, parent = null) {
    items.forEach(item => {
      flat.push({
        ...item,
        parent: parent?.id || null
      })
      if (item.children) {
        traverse(item.children, item)
      }
    })
  }

  traverse(permissions)
  return flat
}
